package com.jason.webserver.controller;

import com.jason.dubbo.api.DubboService;
import com.jason.dubbo.api.HelloService;
import com.jason.dubbo.api.TpService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
public class DubboServerController {

    @Reference
    private HelloService helloService;

    @Reference
    private DubboService dubboService;

    @Reference
    private TpService tpService;

    @RequestMapping("/dubbo/server")
    public String dubboServer(HttpServletRequest request) {
        return helloService.sayHello() + dubboService.dubboServerTest();
    }

    @RequestMapping("/dubbo/tp")
    public String tpServer() {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int num = 0; num < 5; num ++) {
            executorService.submit(() -> {
                for (int i = 0; i <= 1000; i ++) {
                    helloService.sayHello();
                    dubboService.dubboServerTest();
                    tpService.tpTime();
                }
            });
        }
        return "success";
    }
}
